<?php
namespace app\mobile\controller;

use app\model\Theme; 
use app\model\Worker; 

class WShoprank extends Base
{
    public function W_shoprank()
    {
global $_G, $_P;
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('管理后台地址不正确');
}
$system=$_G['system'];
$theme = Theme::getThemeById($themeid);
$config = 	Theme::getConfig($themeid);
if(empty($theme['id'])){
	return_msg('管理后台地址不正确');
}
$myworker = Worker::getThemeWorkerByOpenidNew($_G['openid'],$themeid);
if(!($system['ruanjian']!=3 || $config['showshop']==0 || ($system['ruanjian']==3 && $config['showshop']==$myworker['company']))){
	$myworker = "";
}
if (empty($myworker)) {
	return_msg('您不是员工,无权访问');
}
$msg = Worker::gethoutaistats();
if (!empty($msg)) {
	return_msg($msg);
}


$do = 'w_shoprank';
$paixu = $_P['paixu'];
if ($paixu == 'xfs') {
	$paixutxt = "order by newfans desc,record desc,s.displayorder,s.id";
} elseif ($paixu == 'zf') {
	$paixutxt = "order by forward desc,record desc,s.displayorder,s.id";
} elseif ($paixu == 'll') {
	$paixutxt = "order by scansum desc,record desc,s.displayorder,s.id";
} elseif ($paixu == 'fee') {
	$paixutxt = "order by fee desc,record desc,s.displayorder,s.id";
} elseif ($paixu == 'xx') {
	$paixutxt = "order by recordxx desc,record desc,s.displayorder,s.id";
} else {
	$paixutxt = "order by record desc,s.displayorder,s.id";
}

if (!($myworker['level'] == '1' || ($myworker['level'] == '2' && $config['isshoprank']==1))){
	return_msg('无权访问');
}
$notworker = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
	WHERE status > 0  and status!=3 and workerid = '' and themeid = '{$themeid}' " );
if ($config['showalldata']==0) {
	$noalldata = " and r.themeid =" .$themeid;
}else{
	$noalldata = " ";
}
// $company = db_select("SELECT w.company,s.name as name, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
// 	left join " . tablename ( t_shangjia ) . " s on w.company = s.id
// 	left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
// 	WHERE 1=1 " . $noalldata . " group by w.company " . $paixutxt );

// $sql1 = "SELECT w.company,s.name as name, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
// 	left join " . tablename ( t_shangjia ) . " s on w.company = s.id
// 	left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
// 	WHERE 1=1 " . $noalldata . " and r.riqi='";
// $sql2 = "' group by w.company " . $paixutxt;

// $companytoday = db_select($sql1.date("Y-m-d").$sql2);
// $companyyesterday = db_select($sql1.date("Y-m-d",strtotime("-1 day")).$sql2);
// $companybeforeyesterday = db_select($sql1.date("Y-m-d",strtotime("-2 day")).$sql2);

		$sql1 = "SELECT w.company,s.name as name, sum(r.newfans) as newfans,sum(r.record) as record,sum(r.recordxx) as recordxx,sum(r.recordzx) as recordzx,sum(r.fee) as fee,sum(r.scansum) as scansum,sum(r.forward) as forward, sum(r.refundfee) as refundfee,sum(r.refund) as refund FROM " . tablename ( t_worker ) . " w
			left join " . tablename ( t_shangjia ) . " s on w.company = s.id
			left join " . tablename ( t_workerreport ) . " r on w.uid = r.workerid and w.themeid = r.themeid
			WHERE 1=1 " . $noalldata ;
		$sql2 = " group by w.company " . $paixutxt;

		$company = db_select($sql1.$sql2);
		$companytoday = db_select($sql1 . " and r.riqi='".date("Y-m-d")."' ". $sql2);
		$companyyesterday = db_select($sql1 . " and r.riqi='".date("Y-m-d",strtotime("-1 day"))."' ". $sql2);
		$companybeforeyesterday = db_select($sql1 . " and r.riqi='".date("Y-m-d",strtotime("-2 day"))."' ". $sql2);


if($_G['companyname'] =='新飞驰软件'){
	$return_clicent = get_defined_vars();
return view('/fcworker/fc_shoprank' , $return_clicent);
}else{
	$return_clicent = get_defined_vars();
return view('/yxworker/w_shoprank' , $return_clicent);
}
}
}